/**
 * 气体监测模拟数据
 * 提供模拟的气体监测数据，用于开发和测试
 */

// 存储前一次的气体值，用于生成平滑变化
let previousGasValues = {
    "磷化氢": "0.079",
    "氧气": "20.9",
    "二氧化碳": "0.03"
};

/**
 * 获取模拟的气体监测配置数据
 * @returns {Object} 模拟的气体监测配置数据，格式如 {"SIEMENS_S7": [...]}
 */
function getMockGasConfigData() {
    return {
        "SIEMENS_S7": [
            {
                "address": "40004",
                "dangerValue": 10.0,
                "description": "磷化氢浓度",
                "englishName": "PH₃",
                "maxValue": 100.0,
                "minValue": 0.0,
                "name": "磷化氢",
                "scaleFactor": 10.0,
                "unit": "PPM",
                "warningValue": 5.0
            },
            {
                "address": "40013",
                "dangerValue": 18.0,
                "description": "氧气浓度",
                "englishName": "O₂",
                "maxValue": 25.0,
                "minValue": 0.0,
                "name": "氧气",
                "scaleFactor": 10.0,
                "unit": "%",
                "warningValue": 19.0
            },
            {
                "address": "40017",
                "dangerValue": 1.0,
                "description": "二氧化碳浓度",
                "englishName": "CO₂",
                "maxValue": 5.0,
                "minValue": 0.0,
                "name": "二氧化碳",
                "scaleFactor": 10.0,
                "unit": "%",
                "warningValue": 0.5
            }
        ],
        "MODBUS_TCP": [
            {
                "address": "30001",
                "dangerValue": 10.0,
                "description": "磷化氢浓度",
                "englishName": "PH₃",
                "maxValue": 100.0,
                "minValue": 0.0,
                "name": "磷化氢",
                "scaleFactor": 10.0,
                "unit": "PPM",
                "warningValue": 5.0
            },
            {
                "address": "30003",
                "dangerValue": 18.0,
                "description": "氧气浓度",
                "englishName": "O₂",
                "maxValue": 25.0,
                "minValue": 0.0,
                "name": "氧气",
                "scaleFactor": 10.0,
                "unit": "%",
                "warningValue": 19.0
            }
        ]
    };
}

/**
 * 生成符合现实数值范围的气体值
 * @param {Object} prevValues 前一次的值，用于生成平滑变化
 * @returns {Object} 模拟的气体实时值
 */
function generateRealisticGasValues(prevValues = {}) {
    // 磷化氢：通常在0-0.5 ppm之间的微量，使用小的变化幅度
    const ph3Prev = parseFloat(prevValues["磷化氢"] || "0.079");
    // 在前值基础上小幅变化，保持在0-0.5范围内
    let ph3 = ph3Prev + (Math.random() * 0.02 - 0.01);
    ph3 = Math.max(0, Math.min(0.5, ph3)).toFixed(3);
    
    // 氧气：正常空气中约为20.9%左右，小幅波动
    const o2Prev = parseFloat(prevValues["氧气"] || "20.9");
    // 在前值基础上小幅变化，保持在19-22范围内
    let o2 = o2Prev + (Math.random() * 0.2 - 0.1);
    o2 = Math.max(19, Math.min(22, o2)).toFixed(1);
    
    // 二氧化碳：正常空气中约为0.04%左右，小幅波动
    const co2Prev = parseFloat(prevValues["二氧化碳"] || "0.03");
    // 在前值基础上小幅变化，保持在0.02-0.1范围内
    let co2 = co2Prev + (Math.random() * 0.01 - 0.005);
    co2 = Math.max(0.02, Math.min(0.1, co2)).toFixed(2);
    
    return {
        "磷化氢": ph3,
        "氧气": o2,
        "二氧化碳": co2
    };
}

/**
 * 获取模拟的气体监测数据
 * @returns {Object} 完整的模拟气体监测数据对象
 */
function getMockGasData() {
    // 当前时间戳
    const now = new Date();
    const timestamp = now.getTime();
    const timeString = formatDateTime(now);
    
    // 生成实时值
    const realTimeValues = generateRealisticGasValues(previousGasValues);
    // 更新存储的前一次值
    previousGasValues = realTimeValues;

    // 创建符合标准格式的模拟数据
    return {
        code: 0,
        msg: "模拟数据",
        data: [
            {
                deviceId: 1,
                deviceName: "气体检测仪A",
                isActive: true,
                pointData: [
                    {
                        name: "磷化氢",
                        symbol: "PH₃",
                        value: realTimeValues["磷化氢"],
                        unit: "ppm",
                        address: "DB1.0",
                        timestamp: timestamp,
                        timeString: timeString
                    },
                    {
                        name: "氧气",
                        symbol: "O₂",
                        value: realTimeValues["氧气"],
                        unit: "%",
                        address: "DB1.2",
                        timestamp: timestamp,
                        timeString: timeString
                    },
                    {
                        name: "二氧化碳",
                        symbol: "CO₂",
                        value: realTimeValues["二氧化碳"],
                        unit: "%",
                        address: "DB1.4",
                        timestamp: timestamp,
                        timeString: timeString
                    }
                ]
            },
            {
                deviceId: 2,
                deviceName: "气体检测仪B",
                isActive: true,
                pointData: [
                    {
                        name: "磷化氢",
                        symbol: "PH₃",
                        value: (parseFloat(realTimeValues["磷化氢"]) * 0.9).toFixed(3),
                        unit: "ppm",
                        address: "DB1.0",
                        timestamp: timestamp,
                        timeString: timeString
                    },
                    {
                        name: "氧气",
                        symbol: "O₂",
                        value: (parseFloat(realTimeValues["氧气"]) * 1.02).toFixed(1),
                        unit: "%",
                        address: "DB1.2",
                        timestamp: timestamp,
                        timeString: timeString
                    },
                    {
                        name: "二氧化碳",
                        symbol: "CO₂",
                        value: (parseFloat(realTimeValues["二氧化碳"]) * 1.1).toFixed(2),
                        unit: "%",
                        address: "DB1.4",
                        timestamp: timestamp,
                        timeString: timeString
                    }
                ]
            }
        ],
        timestamp: timestamp
    };
}

/**
 * 获取模拟的标准气体监测数据（可直接用于渲染）
 * @returns {Array} 可直接用于渲染的气体监测数据数组
 */
function getMockGasDataForRendering() {
    const mockData = getMockGasData();
    return [
        {
            deviceId: "SIEMENS_S7",
            deviceName: "气体监测仪",
            isActive: true,
            pointData: mockData.data[0].pointData
        }
       
    ];
}

/**
 * 获取气体实时数据
 * @returns {Promise<Object>} 气体实时值
 */
function getMockGasRealTimeValues() {
    return new Promise((resolve, reject) => {
        try {
            // 生成新的模拟值
            const mockValues = generateRealisticGasValues(previousGasValues);
            
            // 更新本地存储的前一次值
            previousGasValues = mockValues;
            
            resolve(mockValues);
        } catch (error) {
            console.error('获取气体实时值失败:', error);
            reject(error);
        }
    });
}


